%% Copyright 2003 Paul Pichaureau for the MathDesign Project
%% All rights reserved.
%%
%% --------------------------------------------------
%%
%% This program is free software; you can redistribute it and/or
%% modify it under the terms of the GNU General Public License
%% as published by the Free Software Foundation; either version 2
%% of the License, or (at your option) any later version.

%% This program is distributed in the hope that it will be useful,
%% but WITHOUT ANY WARRANTY; without even the implied warranty of
%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%% GNU General Public License for more details.

%% You should have received a copy of the GNU General Public License
%% along with this program; if not, write to the Free Software
%% Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
%% Public LaTeX Project Public

%% This file defines generic macros.

%% For more information about options and configuration, please read
%% the general MathDesign documentation mddoc.pdf.

\NeedsTeXFormat{LaTeX2e}

\ProvidesPackage{mathdesign}
[2006/01/29 v1.55 Math Design Project]

%% -------------------------------------------------------------------
%% Code to use a key-val like syntax in options
%% -------------------------------------------------------------------

\RequirePackage{keyval}
\RequirePackage{ifthen}

\@ifundefined{MD@unusedlist}{%
  \let\MD@unusedlist\@empty}{}
\@ifundefined{MD@extractkey}{%
  \def\MD@extractkey#1=#2\MD@extractkey{#1}}{}
\newcommand{\ProcessUnusedOptions}[1]{%
  \let\MD@tempa\@empty
  \let\MD@tempb\@empty
  \@for\CurrentOption:=\MD@unusedlist\do{%
    \expandafter\expandafter\expandafter\def
    \expandafter\expandafter\expandafter\MD@key
    \expandafter\expandafter\expandafter{%
      \expandafter\MD@extractkey\CurrentOption=\MD@extractkey}%
    \@ifundefined{KV@#1@\MD@key}{%
      \edef\MD@tempa{\MD@tempa,\CurrentOption,}}{%
      \edef\MD@tempb{\MD@tempb,\CurrentOption,}}}%
  \@for\CurrentOption:=\MD@tempa\do{%
    \ifx\CurrentOption\@empty\else
      \PackageWarning{mathdesign}{`\CurrentOption' option not defined.}\fi}%
  \edef\MD@tempb{%
    \noexpand\setkeys{#1}{\MD@tempb}}%
  \MD@tempb
  \AtEndOfPackage{\let\@unprocessedoptions\relax}}

\DeclareOption*{%
  \expandafter\expandafter\expandafter\def
  \expandafter\expandafter\expandafter\MD@unusedlist
  \expandafter\expandafter\expandafter{%
    \expandafter\MD@unusedlist\expandafter,%
    \CurrentOption}}


%% -------------------------------------------------------------------
%% Options
%% -------------------------------------------------------------------

%% Avalaible families

\DeclareOption{utopia}{\gdef\@MDfont{mdput}}
\DeclareOption{mdput}{\gdef\@MDfont{mdput}}
\DeclareOption{adobe-utopia}{\gdef\@MDfont{mdput}}

\DeclareOption{garamond}{\gdef\@MDfont{mdugm}}
\DeclareOption{mdugm}{\gdef\@MDfont{mdugm}}
\DeclareOption{urw-garamond}{\gdef\@MDfont{mdugm}}

\DeclareOption{charter}{\gdef\@MDfont{mdbch}}
\DeclareOption{mdbch}{\gdef\@MDfont{mdbch}}
\DeclareOption{bitstream-charter}{\gdef\@MDfont{mdbch}}

\DeclareOption{mdfga}{\gdef\@MDfont{mdfga}}
\DeclareOption{fontsite-garamond}{\gdef\@MDfont{mdfga}}

%% Activate expert fonts
\newif\if@MDexpert
\DeclareOption{expert}{\@MDexperttrue}

%% Slanted or upright version of various fonts
                                
\newif\if@MDcapupright \@MDcapuprightfalse
\newif\if@MDgrupright  \@MDgruprightfalse  
\newif\if@MDGRupright  \@MDGRuprighttrue   

\define@key{MD}{uppercase}[italicized]{%
    \ifthenelse{\equal{#1}{italicized}}{\@MDcapuprightfalse}
    {\ifthenelse{\equal{#1}{upright}}{\@MDcapuprighttrue}{}}}

\define@key{MD}{greekuppercase}[upright]{%
    \ifthenelse{\equal{#1}{italicized}}{\@MDGRuprightfalse}
    {\ifthenelse{\equal{#1}{upright}}{\@MDGRuprighttrue}{}}}

\define@key{MD}{greeklowercase}[italicized]{%
    \ifthenelse{\equal{#1}{italicized}}{\@MDgruprightfalse}
    {\ifthenelse{\equal{#1}{upright}}{\@MDgruprighttrue}{}}}

%% euro symbol support

\newif\if@MDeuro\@MDeurotrue
\define@key{MD}{euro}[true]{%
    \ifthenelse{\equal{#1}{true}}{\global\@MDeurotrue}
    {\global\@MDeurofalse}}

%% scaled sans serif and typewritefonts

\newif\if@MDsfscaled\@MDsfscaledtrue
\define@key{MD}{sfscaled}[true]{%
    \ifthenelse{\equal{#1}{true}}{\global\@MDsfscaledtrue}
    {\global\@MDsfscaledfalse}}

\newif\if@MDttscaled\@MDttscaledtrue
\define@key{MD}{ttscaled}[true]{%
    \ifthenelse{\equal{#1}{true}}{\global\@MDttscaledtrue}
    {\global\@MDttscaledfalse}}

%% Type of calligraphic alphabet

\newif\if@MDcmcal  \@MDcmcalfalse

\define@key{MD}{cal}[cmcal]{%
    \ifthenelse{\equal{#1}{cmcal}}{\@MDcmcaltrue }
    {\ifthenelse{\equal{#1}{script}}{\@MDcmcalfalse}{}}}

\ProcessOptions

\@ifundefined{@MDfont}
{\PackageWarning{mathdesign}{No font specified}}
{\InputIfFileExists{mathdesign.cfg}{}{}
    \InputIfFileExists{\@MDfont.cfg}{}{}
    \ExecuteOptions{\@MDfont Options}}

\ProcessUnusedOptions{MD}

%% ----------------------------------------------------------------
%% Detects possible package incompatibility
%% ----------------------------------------------------------------

\def\WarningIfLoaded#1#2{%
    \AtBeginDocument{\@ifpackageloaded{#2}{\PackageWarning{mathdesign/#1}{%
            Package '#2' shouldn't be used in conjonction with
            package #1,}}{}}}

\def\WarningIfLoadedNoOption#1#2{%
    \AtBeginDocument{
        \@ifpackageloaded{#2}{\PackageWarning{mathdesign/#1}{%
            Package '#2' shouldn't be used in conjonction with
            package #1 (use option #2 instead)}}{}}}
    

%% ----------------------------------------------------------------
%% Undefines symbol name before definition
%% ----------------------------------------------------------------

\def\MD@def#1#2{\global\let#2=\@undefined #1#2}

%% ----------------------------------------------------------------
%% Some 'faked' symbols
%% ----------------------------------------------------------------

% Redefine \textcircled
\DeclareTextCommand{\textcircled}{OMS}[1]{\hmode@bgroup
   \ooalign{%
      \hfil \hbox {\upshape#1}\hfil \crcr
      \char 13 % "0D
   }%
 \egroup}

%Extra circled stuff
\DeclareTextAccentDefault{\figurecircled}{MDB}
\DeclareTextCommand{\figurecircled}{MDB}[1]{\hmode@bgroup
   \ooalign{%
      \hfil \hbox {\upshape#1}\hfil \crcr
      \char 64
   }%
 \egroup}

%% -------------------------------------------------------------------
%% Define or undefine small caps
%% -------------------------------------------------------------------

\if@MDexpert\else\renewcommand\scdefault{fsc}\fi

\newcommand{\fscshape}[1]{\fontshape{fsc}\selectfont #1}
\newcommand{\ficshape}[1]{\fontshape{fic}\selectfont #1}


%% -------------------------------------------------------------------
%% Some tweaking
%% -------------------------------------------------------------------

\delimiterfactor950

%% -------------------------------------------------------------------
%% Load the family style file
%% -------------------------------------------------------------------
\@ifundefined{@MDfont}
  {}
  {\RequirePackage{\@MDfont}}

%% ----------------------------------------------------------------
%% Encoding is automatically set to T1
%% ----------------------------------------------------------------

\RequirePackage[T1]{fontenc}

\endinput

